package files;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.Set;

/** Recursive listing with SimpleFileVisitor in JDK 7. */

public class ProcessFile extends SimpleFileVisitor<Path> {
	
	private Set<File> list = new HashSet<File>();
	
	private String root = "";
	private boolean subFolders = true;
	
	public ProcessFile(String root, boolean subFolders) throws IOException {
		this.root = root;
		this.subFolders = subFolders;
		Files.walkFileTree(Paths.get(root), this);
	}
	
	@Override
	public FileVisitResult visitFile(Path aFile, BasicFileAttributes aAttrs) throws IOException {
		list.add(aFile.toFile());
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult preVisitDirectory(Path aDir, BasicFileAttributes aAttrs) throws IOException {
		if(subFolders == false) {
			if(aDir.toString().equals(root))
				return FileVisitResult.CONTINUE;
			else
				return FileVisitResult.SKIP_SUBTREE;
		}
		else
			return FileVisitResult.CONTINUE;
	}

	public Set<File> getList() { return list; }
	public void setList(Set<File> list) { this.list = list; }
	
	
	public static void main(String... aArgs) throws IOException{
		String root = "K:\\(｡･｀ω´･｡)\\2d\\kemono";
		FileVisitor<Path> fileProcessor = new ProcessFile(root, true);
		
		/*for(File f : ((ProcessFile) fileProcessor).getList()) {
			System.out.println(f.getParentFile().getName());
		}*/
		System.out.println(((ProcessFile) fileProcessor).getList().size());
	}
	
}
	